<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core\lang;
use classlib\core as C;
use classlib\core\controller;
class Cview extends controller {
	//工作流引擎模板设置
	public function main() {
		$view = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('workflow/viewform.ext.js');
	}

	/**
	 * @desc    备份流程表单资料信息
	 * @doc     备份流程表单
	 * @access  需要授权
	 */
	public function dobackup() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$list  = C\model::load('main.proc.nodeform')->get_list(0, -1);
		$dir   = COREDIR.'/cachedir/workflow/';
		if (!is_dir($dir)) {
			mkdir($dir, 0777, true) or die('Access Deny!');
		}
		$str   = json_encode($list);
		$file  = $dir.'form.'.date('Ymd').'.js';
		if (file_exists($file)) {
			unlink($file);
		}
		file_put_contents($file, $str);
		$view->display();
	}

	/**
	 * @desc    保存流程资料信息
	 * @doc     保存流程表单
	 * @access  需要授权
	 */
	public function dosave() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$id    = (int)input::get_post('id');
		$data  = array('stime'=>C\config::$nowtime);
		$filter= new \classlib\validator\auditor();
		$lang  = C\lang::inst('adm');
		$filter->lang($lang);
		try {
			$data['name']     = $filter->reset('name')->required()->maxlen(127)->run();
			$data['panelobj'] = $filter->reset('panelobj')->required()->run();
			$data['panelbtn'] = $filter->reset('panelbtn')->required()->run();
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		if ($id > 0) {
			C\model::load('main.proc.nodeform')->save($id, $data);
		} else {
			C\model::load('main.proc.nodeform')->new_one($data);
		}
		$view->display();
	}

	/**
	 * @desc    删除流程表单资料信息
	 * @doc     删除流程表单
	 * @access  需要授权
	 */
	public function dodelete() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$id    = (int)input::get_post('id');
		if($id < 0) {
			$view->assign_error('50001', $lang->get('workflow', '50001'));
		}
		$exists= C\model::load('main.proc.node')
			->isexist(array('formid'=>$id));
		if ($exists) {
			$view->assign_error('50002', $lang->get('workflow', '50002'));
		}
		C\model::load('main.proc.nodeform')->delete($id);
		$view->display();
	}

	//获取工作流引擎视图列表
	public function dolist() {
		$view  = C\view::inst('json');
		Cadmin::chkaccess($view, 'workflow/view');
		list($start, $limit, $sort) = Cadmin::offset();
		$where = array();
		$name  = input::post('name');
		if(!$name) {
			$where[]= array('name', '%'.$name.'%', \classlib\orm::OP_LIKE);
		}
		$orm   = C\model::load('main.proc.nodeform');
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$list = $orm->get_list($start, $limit, $where, '*', $sort);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
	
	//共享的表单资料信息
	public function doshare() {
		$view  = C\view::inst('json');
		Cadmin::islogin($view);
		list($start, $limit, $sorts) = Cadmin::offset();
		$where = array();
		$query = input::post('query');
		if ($query) {
			$where[] = array('name', '%'.$query.'%', \classlib\orm::OP_LIKE);
		}
		$orm   = C\model::load('main.proc.nodeform');
		$total = $orm->get_total($where);
		$list  = array();
		if ($total > 0) {
			$field = 'id, name';
			$list  = $orm->get_list($start, $limit, $where, $field, $sorts);
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}
